package com.slz3.business.module.systemManagement.sysRoleUser.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.systemManagement.sysRoleUser.domain.SysRoleUser;
import com.slz3.business.module.systemManagement.sysRoleUser.domain.SysRoleUserFactory;
import com.slz3.business.module.systemManagement.sysRoleUser.steward.SysRoleUserSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class SysRoleUserServiceImpl implements SysRoleUserService {
    @Override
    public SysRoleUser save(SysRoleUser sysRoleUser) {
        return Repositorys.sysRoleUser.save(SysRoleUserFactory.initBaseFields(sysRoleUser));
    }
    @Override
    public Page<SysRoleUser> page(MultiValueMap<String, String> params) {
        Page<SysRoleUser> sysRoleUserPage = Repositorys.sysRoleUser.findAll(
                SysRoleUserSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return sysRoleUserPage;
    }


    @Override
    public SysRoleUser modify(SysRoleUser sysRoleUserNew) {
        return Repositorys.sysRoleUser.saveAndFlush(SysRoleUserFactory.initBaseFields(sysRoleUserNew));
    }

    @Override
    public Optional<SysRoleUser> single(UUID uuid) {
        return Repositorys.sysRoleUser.findById(uuid);
    }

    @Override
    public List<SysRoleUser> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.sysRoleUser.findAllById(uuids);
    }

    @Override
    public List<SysRoleUser> delete(List<SysRoleUser> sysRoleUserList) {
        sysRoleUserList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SysRoleUserFactory.initBaseFields(item);
        });
        return Services.sysRoleUserTransactional.batchModify(sysRoleUserList);
    }

}
